package newnes.io.memory;

import newnes.io.memory.MappedMemory;
import java.nio.ByteBuffer;

public class Ram extends MappedMemory {

	public Ram(){
		super();
	}
	
	public Ram(int size){
		super(size);
	}
	
	public int writeToMemory(int address, byte value){
		Memory hook = getMemoryHook(address);
		if(hook==null){
			ByteBuffer memory_buffer = getMemoryBuffer();
			int buffer_location = address-getAddressSpace().getStartAddress();
			buffer_location = buffer_location & getAddressMask();
			memory_buffer.put(buffer_location, value);
			return 1;
		}
		else{
			return hook.writeToMemory(address, value);
		}
	}
	
	public int readFromMemory(int address){
		Memory hook = getMemoryHook(address);
		if(hook == null)
		{
			ByteBuffer memory_buffer = getMemoryBuffer();
			int buffer_location = address-getAddressSpace().getStartAddress();
			buffer_location = buffer_location & getAddressMask();
			return memory_buffer.get(buffer_location);
		}else{
			return hook.readFromMemory(address);
		}
	}	
}
